package com.donn.securitycontrolplatform.mqtt;


import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


@Component
public abstract class MqttReceiveHandle implements MqttCallback {

    private final Logger logger = LoggerFactory.getLogger(MqttReceiveHandle.class);

    public abstract void handle(String topic, Message<?> message);

    @Override
    public void connectionLost(Throwable throwable) {
        logger.warn("连接丢失");
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        logger.info("消息到达:" + topic + "\n" + "消息内容：" + new String(mqttMessage.getPayload()) + "\nclientId：" + mqttMessage.getId());
    }


    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        logger.info("clientId:" + iMqttDeliveryToken.getClient().getClientId());
    }

}
